package com.cci.kangdao.controller.hhp;

import com.cci.kangdao.dto.request.AttendanceRequest;
import com.cci.kangdao.dto.request.AttendanceUserStatisticsRequest;
import com.cci.kangdao.service.AttendanceRecordService;
import com.cci.kangdao.utilTool.JsonUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @description: 考勤记录接口
 * @author: zyf
 * @create: 2021-05-24
 */
@Controller
public class AttendanceRecordController {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private AttendanceRecordService recordService;

    /**
     * 查询用户当前天的打卡记录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getAttendance.aspx" ,method = RequestMethod.GET)
    public void getAttendance(HttpServletRequest request, HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            recordService.getAttendance(map,request,response);
        }catch(Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    /**
     * 更新用户当前打卡记录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/uploadAttendance.aspx",method = RequestMethod.POST)
    public ModelAndView uploadAttendance(@RequestBody String body , HttpServletRequest request, HttpServletResponse response){
        try{
            log.info("uploadAttendance.aspx接口入参："+body);
            AttendanceRequest attendanceRequest = JsonUtils.toJavaObject(body,AttendanceRequest.class);
            recordService.uploadAttendance(attendanceRequest,request,response);
        }catch(Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }


    /**
     * 查询个人考勤统计表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getUserAttendanceList.aspx",method = RequestMethod.GET)
    public ModelAndView getAttendanceList( AttendanceUserStatisticsRequest  statisticsRequest, HttpServletRequest request, HttpServletResponse response){
        try{
            recordService.getAttendanceUserStatistics(statisticsRequest,request,response);
        }catch(Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

}
